#!/bin/bash
set -e
if [ -z $CLOONIX_CONFIG ]; then
  CLOONIX_CONFIG=/usr/local/bin/cloonix/cloonix_config
fi

if [ ! -f $CLOONIX_CONFIG ]; then
  echo "ERROR: $CLOONIX_CONFIG not found"
  exit 1
fi
CLOONIX_TREE=$(cat $CLOONIX_CONFIG |grep CLOONIX_TREE | awk -F = '{print $2}')
CLOONIX_WORK=$(cat $CLOONIX_CONFIG |grep CLOONIX_WORK | awk -F = '{print $2}')
CLOONIX_WORK=$(eval echo $CLOONIX_WORK)



if [ ! -d ${CLOONIX_TREE} ]; then
  echo "ERROR: ${CLOONIX_TREE} not found"
  exit 1
fi

LIST=$(cat $CLOONIX_CONFIG |grep CLOONIX_NET: | awk '{print $2}')

if (( ${#} < 1 )); then
  echo  cloonix_name needed as parameter.
  echo $LIST
  exit 1
fi

cloonix_name=$1

found=no
for i in $LIST ; do
  if [ "$cloonix_name" == "$i" ]; then
    found=yes
  fi
done

if [ "$found" == "no" ]; then
  echo "ERROR: $cloonix_name not found in $CLOONIX_CONFIG"
  echo $LIST
  exit 1
fi

if (( ${#} < 3 )); then
  echo error input params
  echo  "cloonix_osh <nemo> <nat> <user@vm_name> [cmd]"
  exit 1
fi
nat_name=$2
user_and_vm=$3
user_name=${user_and_vm%%@*}
vm_name=${user_and_vm##*@}

LINE_CLOONIX_CONFIG=$(cat $CLOONIX_CONFIG)
LINE_CLOONIX_CONFIG=$(echo $LINE_CLOONIX_CONFIG)
cloonix_info="${LINE_CLOONIX_CONFIG##*CLOONIX_NET: $cloonix_name \{}"
cloonix_info="${cloonix_info%%\}*}"

ip=$(echo $cloonix_info |awk '{print $2}')
port=$(echo $cloonix_info |awk '{print $4}')
passwd=$(echo $cloonix_info |awk '{print $6}')

set +e
get_ip=$(cloonix_cli ${cloonix_name} cnf sat ${nat_name} "whatip ${vm_name}")
resp=${get_ip%% *}
ip_vm=${get_ip##* }
if [ "${resp}" != "RESPOK" ]; then
  echo ${get_ip}
  exit
fi

PARAMS="${ip}=${port}=${passwd}=${nat_name}@user=${user_name}=ip=${ip_vm}=port=22=cloonix_info_end"
SOCK=${CLOONIX_WORK}/${cloonix_name}/cli
if [ ! -e $SOCK ]; then
  mkdir -p $SOCK
fi

shift 3

BIN_SSH=${CLOONIX_TREE}/client/openssh/cloonix_u2i_ssh
#${BIN_SSH} ${SOCK} -i ${CLOONIX_TREE}/id_rsa -X ${PARAMS} "$@"
${BIN_SSH} ${SOCK} -i ${CLOONIX_TREE}/id_rsa ${PARAMS} "$@"

